let app = getApp();
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    items: {
      type: Object,
      value: {},
      observer(newVal, oldVal, changedPath) {

      }
    },
    index: {
      type: Number,
      value: null,
      observer(newVal, oldVal, changedPath) {

      }
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    number: 0,
    tag_icon: [
      '/images/goods/80fd4df058be21c0fd3bcda725cb6e52.jpg',
      '/images/goods/f786df07ea67e2c0abc2b4add735b9fa.jpg',
      '/images/goods/8fc0685e71d6d12b8c2021fb2d7cd69d.jpg',
      '/images/goods/330b1fe80687cb88ce88dc5583cd10c6.jpg',
      '/images/goods/942db5c01707a94b490a6d6853ec98d7.jpg',
      '/images/goods/c5e0407be68c766e17cc10d1acb4cde6.jpg',
    ],
    tag_text: [
      '/images/goods/a11489211564c1d91bfcd96c10774f53.jpg',
      '/images/goods/6e86041df905d894abcae7b53e0dccd3.jpg',
      '/images/goods/408802ce578ee543b1d86122ea068c19.jpg'
    ],
    points: "http://jzsh-test.oss-cn-shenzhen.aliyuncs.com/data/upload/images/2019-10-30/9ff6a7e4b740d791771f3f522d4bf8b3.png"
  },
  ready() {

  },
  /**
   * 组件的方法列表
   */
  methods: {
    // 添加购物车
    addOneCart(e) {
      let that = this;
      let item = e.currentTarget.dataset.item;
      // 本平台
      if (item.hasoption == 1) {
        that.triggerEvent('addCart', { goods: item, index: that.data.index, platform: 'self' });
        return;
      }
      that.ajaxAddCart(that.data.index, item);
    },
    // 数量改变
    updateCart(e) {
      let that = this;
      let total = e.detail;
      let item = e.currentTarget.dataset.item
      if (total > item.cartNum) {
        that.ajaxAddCart(that.data.index, item);
      } else {
        that.ajaxAddCart(that.data.index, item, false);
      }
    },
    // 添加商品
    ajaxAddCart(index, goods, plus = true) {
      let that = this;
      let userinfo = app.functions.getLocalStorage("user");
      let shop = app.functions.getLocalStorage("shop");
      if (app.functions.isEmpty(userinfo)) {
        app.functions.islogin();
        return false;
      }
      let cart = {
        supplyChannel:1,
        mid: userinfo.id,
        teamid: shop.id,
        productId: goods.id,
        total: plus ? 1 : -1,
        marketprice: goods.marketprice
      };
      //修改吉贡单规格商品
      if (goods.skus.length == 1) {
        let skusOne = goods.skus[0];
        cart.skuId = skusOne.id;
        cart.skuTitle = skusOne.title;
        cart.marketprice = (userinfo.isVip && skusOne.vipprice && goods.isvip) ? skusOne.vipprice : skusOne.marketprice;
        cart.vipprice = skusOne.vipprice;
      }
      if (goods.maxbuy && (goods.cartNum + cart.total) > goods.maxbuy) {
        app.functions.showToast("已经超过单次购买数量");
        return;
      }
      app.api.cartAdd(cart, (response) => {
        if (response.code != 0) {
          app.functions.showToast("加入到购物车失败");
          return;
        }
        wx.vibrateShort();
        // app.functions.showToast("已加入到购物车");
        app.cartNumber = app.cartNumber == 0 ? cart.total : parseInt(app.cartNumber) + cart.total;
        that.setData({
          ["items.cartNum"]: this.data.items.cartNum + cart.total
        })
        app.UpdateCart();
      }, (error) => {

      })
    },
    // 分享
    toshare(e) {
      let pages = getCurrentPages(); //获取加载的页面
      let currentPage = pages[pages.length - 1]; //获取当前页面的对象
      let url = currentPage.route;//当前页面url
      app.cache[url].getProductsShare(e.currentTarget.dataset.goods);
    },
    // 播放视频
    getPlayVideo() {
      let pages = getCurrentPages(); //获取加载的页面
      let currentPage = pages[pages.length - 1]; //获取当前页面的对象
      let url = currentPage.route;//当前页面url
      app.cache[url].playVideo(this.data.items);
    },
    // 跳转到商品详情
    goDetail(e) {
      let goodsid = e.currentTarget.dataset.goodid;
      wx.navigateTo({
        url: '/pages_subpack/goods_detail/goods_detail?goodsid=' + goodsid
      })
    }
  }
})
